import { defineStore } from 'pinia';

export const useUserStore = defineStore('user', {
    actions: {
        // setCourseContainerInfo(key, value) {
        //     this.courseContainerInfo[key] = value
        // },
        // 改变当前登录用户身份
        setIdentity(newIdentity) {
            this.identity = newIdentity
        },
        // 更新token
        setToken(newToken) {
            // 存储到pinia
            this.token = newToken
            // 存储到localStorage
            window.localStorage.setItem('token', newToken)
        },
        // 删除token
        deleteToken() {
            window.localStorage.setItem('token', '')
            this.token = ''
        },
        // 改变验证状态
        setIsVerification() {
            this.isVerification = !this.isVerification
        },
        // 改变信息
        setInfo(info){
            this.info.name = info.name
            this.info.phone = info.phone
            this.info.account = info.account
            this.info.head = info.head
        },
        setHead(headUrl) {
            this.info.head = headUrl
        },
    },


    state: () => ({
        // 身份
        identity: '',
        // token
        token: '',
        info:{
            name:null,
            phone:null,
            account:null,
            head:null
        },
        // 当前是否验证通过
        isVerification: false
    }),

    getters: {
        // 获取token
        getToken() {
            // 判断pinia中的token是否存在
            if (this.token === '') {
                // 不存在就从localStorage中获取
                this.token = window.localStorage.getItem('token')
            }
            return this.token
        },
        getChineseIdentity() {
            const identity ={
                super_admin:'超级管理员',
                teacher:'教师',
                admin:'管理员',
                student:'学生',
            }
            return identity[this.identity]
        }
    },
})
